package com.zjt.smart.app;

import com.zjt.smart.R;
import com.zjt.smart.mvp.model.Event;
import com.zjt.smart.util.StatusUtil;
import com.zjt.smart.util.ToastUtil;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2017</p>
 * <P>Company: </p>
 *
 * @author zhengjianting
 * @time 2017/9/30 0030 下午 3:46
 */

public class GlobalEventHelper {
    /** 从无网切换至Wifi */
    public final static int NO_CHANE = 0;
    /** 从无网切换至Wifi */
    public final static int CHANE_NONE_TO_WIFI = 1;
    /** 从无网切换至手机网络 */
    public final static int CHANE_NONE_TO_MOBILE = 2;
    /** 从Wifi切换至手机网络 */
    public final static int CHANE_WIFI_TO_MOBILE= 3;
    /** 从手机网络切换至Wifi */
    public final static int CHANE_MOBILE_TO_WIFI = 4;
    /** 从有网切换至无网 */
    public final static int CHANE_NETWORK_TO_NONE= 5;

    private MainApplication application;
    private boolean lastAvailable;
    private boolean lastIsWifi;
    private boolean lastIsMobile;

    public GlobalEventHelper(MainApplication application) {
        this.application = application;
        lastAvailable = StatusUtil.isNetworkAvailable(application);
        lastIsWifi = StatusUtil.isWifi(application);
        lastIsMobile = StatusUtil.isMobileNetWork(application);
    }

    public void handleEvent(Event event){
        switch (event.what){
            case Event.GLOBAL_EVENT_LOGIN:
                break;
            case Event.GLOBAL_EVENT_LOGOUT:
                break;
            case Event.GLOBAL_EVENT_NETWORK_CHANGE:
                int networkChangeType = getNetworkChange();
                switch (networkChangeType) {
                    case CHANE_NONE_TO_WIFI:
                    case CHANE_MOBILE_TO_WIFI:
                        break;
                    case CHANE_WIFI_TO_MOBILE:
                    case CHANE_NONE_TO_MOBILE:
                        break;
                    case CHANE_NETWORK_TO_NONE:
                        if (!StatusUtil.isNetworkAvailable(MainApplication.getApp())){
                            ToastUtil.showToast(R.string.no_net);
                        }
                        break;

                    default:
                        break;
                }
                break;
        }
    }

    private int getNetworkChange() {
        boolean currentAvailable = StatusUtil.isNetworkAvailable(application);
        boolean currentIsWifi = StatusUtil.isWifi(application);
        boolean currentIsMobile = StatusUtil.isMobileNetWork(application);
        if (currentAvailable && !lastAvailable) {// 从无网切换到有网
            lastAvailable = true;
            if (currentIsWifi) {
                lastIsWifi = true;
                lastIsMobile = false;
                return CHANE_NONE_TO_WIFI;
            } else {
                lastIsWifi = false;
                lastIsMobile = true;
                return CHANE_NONE_TO_MOBILE;
            }

        } else if (!currentAvailable && lastAvailable) {// 从有网切换到无网
            lastIsWifi = false;
            lastIsMobile = false;
            lastAvailable = false;
            return  CHANE_NETWORK_TO_NONE;
        } else if (currentIsWifi && !lastIsWifi) { // 不同网络间切换
            lastIsWifi = true;
            lastIsMobile = false;
            return CHANE_MOBILE_TO_WIFI;
        } else if (currentIsMobile && !lastIsMobile) { // 不同网络间切换
            lastIsMobile = true;
            lastIsWifi = false;
            return CHANE_MOBILE_TO_WIFI;
        } else {
            return NO_CHANE;
        }
    }
}
